package com.itheima.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.interceptor.AdminHolder;
import com.itheima.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
public class AdminController {

    @Autowired
    AdminManager adminManager;

    //生成验证码图片
    @GetMapping("/system/users/verification")
    public void getVerification(String uuid, HttpServletResponse response) throws IOException {
        //1.调用manager生成图片
        LineCaptcha lineCaptcha = adminManager.getVerification(uuid);

        //2.返回图片(使用response)
        lineCaptcha.write(response.getOutputStream());
    }

    //登陆
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody Map<String,String> map){
        //1.从map中获取四个参数
        String username = map.get("username");
        String password = map.get("password");
        String requestCode = map.get("verificationCode");
        String uuid = map.get("uuid");

        //2.调用manager完成登陆操作
        return adminManager.login(username,password,requestCode,uuid);
    }

    //获取管理员的基本信息
    @PostMapping("/system/users/profile")
    public ResponseEntity findAdminInfo(){
        return ResponseEntity.ok(AdminHolder.get());
    }

    //退出
    @PostMapping("/system/users/logout")
    public void logout(@RequestHeader("Authorization")String token){
        adminManager.logout(token);
    }

}
